package com.shanjupay.transaction.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//响应h5页面，不能使用restController注解，该注解只能响应json串
@Controller
@Slf4j
//支付宝接口测试，在交易服务写http接口 前端进行调用，交易服务进行发送请求支付宝系统进行支付订单下单，
public class PayTestController {

    //应用Id
    String APP_ID = "2021000118633855";
    //应用私钥
    String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCY0Ik8Eg9ejySvxkF7Sx3lwzlj+cBuRxrHOdC4xAVe2jl/XRjYAgVBwg8BdnrqNUO/WqFsx6Ax4HgFu2H+t4YYVy4+G/3LGTqKojdLQQhLyId0zO6h93P/muWAuZZgtsE66ls2W9OOfHguPJcr2GAmBwt/1x2fNqau2SEMTkTRHfw/2UYG6995hAkHx0cs9aEErYTJte3Tv+YTwr6equYPLbe0gSjiZjc0WpPMNmiCwW6oVu5xVNwM47BRA2rc5lil6sV1arRoe1w7w8PAgtnXnWOAFx6/SmqMODzf3J/Z1PIbG3vjhU8jOjj1H9Q0z+LubLyjwJa0sUZKABqMlqkHAgMBAAECggEAcU2tcyyw2Jupx+pngtMfR8yIMOhtFVcDM9RaPaSZlsqW9yg/02ksHKWWGq+OUzlLEgZQyib5UAHu+9EDx6irdECvuL1oX75E+aVs+g8t54K3bJLGHLEtAMgIgnkYVn0qqFQ7bQKIyYzgTuf97jRwlcGDdTY6cGJFFD9JkaY2wBUf9tOR9hUPecifo6inpTcWIpI7pTZuDaDoURrVRMfAyxIPCsxEFtKg9s8QvX4ipVZw5UV2IZBk7u0s5jGWY4xfqcMdXJWVvjQNe2F/RFOJ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A4/QfKQWZYwOL7afy/FgBue5jXF7pXxSTOOGMcjkCH3a/8V+yO3PzKJiUZM138WUGTHhKS0Oo0qVfvzCsjKGhwdPYgbkjzfYAqdHU6GZTnOnW0Mv2+3HREqCipVt1I/uk7pDmBMJz6/0kWpk+NbLOjfA2OSuRcFZOoh5P+CMBf9tMDtcKneuaiPOYEt34FMVc9jT0sE8j6dVGPua+EympZ2sblyOHEVyXi7OUPkWAcOtUB+Ut4ubsRyJ9vpURTGWqRUzSTmFLNJfRScpy1rYQIDAQAB";
    //编码格式：utf-8
    String  CHARSET = "utf-8";
    //支付宝网关地址
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    //支付宝签名算法类型
    String sign_type = "RSA2";

    @GetMapping("/alipaytest")
    public void alipayClient(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //构造支付宝客户端的sdk对象
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,sign_type);
        //创建API对应的request对象
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
        //设置回调地址和通知地址
        //alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
        //alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150420010101017\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数 订单号 和金额  商品标题  product_code

        String form = "";

        try {
            //使用支付宝客户端发送请求给支付宝客户端生成支付订单
            form   =  alipayClient.pageExecute(alipayRequest).getBody();
        }catch (AlipayApiException e){
            e.printStackTrace();
        }

        //设置响应的参数 ，回写响应
        response.setContentType("text/html;charset=" + CHARSET);
        response.getWriter().write(form);
        response.getWriter().flush();
        response.getWriter().close();
    }

}
